﻿using Pay.Dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pay.Model.Model;
using Pay.Dapper;
using Dapper.Contrib.Extensions;
using Dapper;
using Pay.Model.Interfaces;
using Pay.Model.Dto;

namespace Pay.DalImplement
{
    public class OrderDalImplement : IOrderDal
    {
        public async Task<long> Add(OrderInfo model)
        {
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
                return await conn.InsertAsync<OrderInfo>(model);
            }
        }

        public async Task<OrderInfo> GetById(long orderId)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "select * from order_info where id=@id";
                return await conn.QueryFirstAsync<OrderInfo>(sql, new { id = orderId });
            }
        }

        public async Task<bool> Update(OrderInfo model)
        {
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
                return await conn.UpdateAsync<OrderInfo>(model);
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="searchWord"></param>
        /// <param name="merchantId"></param>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="offset"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public async Task<IPagedOutput<OrderInfo>> GetPage(string searchWord, long? merchantId, DateTime? timeBegin = null, DateTime? timeEnd = null, int offset = 10, int rows = 1)
        {
            StringBuilder sb = new StringBuilder("SELECT [Id],[MerchantId],[PayStatus],[Amount],[FeeAmount],[FeeRate],[MchTradeNo],[AmountPay],[CreateTime] from order_info where 1=1");
            DynamicParameters param = new DynamicParameters();
            //param.Add("userId", userId);
            if (!string.IsNullOrWhiteSpace(searchWord))
            {
                sb.Append(" and  MchTradeNo like '%'+@searchWord+'%'");
                param.Add("@searchWord", searchWord);
            }

            if (merchantId.HasValue)
            {
                sb.Append(" and  MerchantId =@MerchantId");
                param.Add("@MerchantId", merchantId.Value);
            }
            if (timeBegin.HasValue && timeEnd.HasValue)
            {
                sb.Append(" and  createtime >=@timeBegin and createTime <=@timeEnd");
                param.Add("@timeBegin", timeBegin.Value);
                param.Add("@timeEnd", timeEnd.Value);

            }

            using (var conn = ConnectionFactory.GetReadConnection())
            {
                return await conn.PagedByAsync<OrderInfo>(sb, param, offset, rows, "createTime", false);
            }
        }
        /// <summary>
        /// 统计分页
        /// </summary>
        /// <param name="searchWord"></param>
        /// <param name="merchantId"></param>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="offset"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public async Task<IPagedOutput<StatisticModel>> GetPageStatistic(string searchWord, long? merchantId, DateTime? timeBegin = null, DateTime? timeEnd = null, int offset = 10, int rows = 1)
        {
            StringBuilder sb = new StringBuilder("SELECT MerchantId as id,Sum(Amount) as totalAmount,Sum(FeeAmount) as totalFee,Sum(AmountPay) as totalPay from order_info where 1=1");
            DynamicParameters param = new DynamicParameters();
            //param.Add("userId", userId);
            if (!string.IsNullOrWhiteSpace(searchWord))
            {
                sb.Append(" and  MchTradeNo like '%'+@searchWord+'%'");
                param.Add("@searchWord", searchWord);
            }

            if (merchantId.HasValue)
            {
                sb.Append(" and  MerchantId =@MerchantId");
                param.Add("@MerchantId", merchantId.Value);
            }
            if (timeBegin.HasValue && timeEnd.HasValue)
            {
                sb.Append(" and  createtime >=@timeBegin and createTime <=@timeEnd");
                param.Add("@timeBegin", timeBegin.Value);
                param.Add("@timeEnd", timeEnd.Value);

            }
            sb.Append(" group by MerchantId");
            try
            {
                using (var conn = ConnectionFactory.GetReadConnection())
                {
                    return await conn.PagedByAsync<StatisticModel>(sb, param, offset, rows, "totalAmount", false);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
